package com.LostAndFound.service;

import java.sql.SQLException;
import java.util.List;

import com.LostAndFound.dao.UserListDao;
import com.LostAndFound.domain.User;
import com.LostAndFound.utils.DataSourceUtils;

public class UserListService {
	UserListDao dao = new UserListDao();

	// 分页
	public int getTotalUserForEasyUI() {
		int total = 0;
		try {
			total = dao.getTotalUserForEasyUI();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return total;
	}
	public List<User> getUserListForEasyUI(int pageNumber, int pageSize) {
		List<User> userList = null;
		int startIndex = (pageNumber-1)*pageSize;
		try {
			userList = dao.getUserListForEasyUI(startIndex,pageSize);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return userList;
	}
	
	
	/* 删除用户，把此用户发布的寻找失物，寻找失主的记录也删除
	 * 1、删除此用户发布的记录但被别人领取的记录，foundArticle中, foundflag=0 and invalidflag=1
	 * 2、删除此用户发布的记录，article中, foundflag=1 and invalidflag=1 or invalidflag=0
	 * 3、删除此用户记录，user中
	 */
	 public boolean handleDeleteUserForEasyUI(String uid) {
		boolean success = false;
		try {
			DataSourceUtils.startTransaction();
			// 1、此用户发布了多少条已经被领取了的article记录
			int deleteTotal = dao.getTotalToDelete(uid);
			Object obj = null;
			for(int i = 0; i < deleteTotal; i++) {
				// 2、从article中选出aid
				obj = dao.selectAidFromArticleToDelete(uid);
				// 3、从foundArticle中删除
				dao.deleteFromFoundArticle(obj);
				// 4、从article中删除
				dao.deleteFromArticle(obj);
			}
			// 5、删除article中已经无效的且uid=uid的记录
			dao.deleteFromArticleForInvalid(uid);
			// 6、删除article中foundflag=1且invalidflag=1的记录
			dao.deleteFromArticleForNotFoundAndValid(uid);
			// 7、删除该用户
			dao.deleteFromArticleForUser(uid);
		
			success = true;
			
		} catch (SQLException e) {
			try {
				DataSourceUtils.rollback();
				success = false;
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				DataSourceUtils.commitAndRelease();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return success;
	}
	public boolean checkAddUsername(String username) {
		int row = 0;
		try {
			row = dao.checkAddUsername(username);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return row>0?true:false;
	}
	public boolean handleAddUserEasyUI(User user) {
		boolean success = false;
		try {
			DataSourceUtils.startTransaction();
			dao.handleAddUserEasyUI(user);
			success = true;
		} catch (SQLException e) {
			try {
				DataSourceUtils.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				DataSourceUtils.commitAndRelease();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return success;
	}
	public boolean handleEditUserEasyUI(User user) {
		boolean success = false;
		try {
			DataSourceUtils.startTransaction();
			dao.handleEditUserEasyUI(user);
			success = true;
		} catch (SQLException e) {
			try {
				DataSourceUtils.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				DataSourceUtils.commitAndRelease();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return success;
	}

}
